package com.zzyl.nursing.controller;

import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.vo.PropertyVo;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.dto.RegisterDto;
import com.zzyl.nursing.service.DeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DevicePageVo;
import com.zzyl.nursing.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/nursing/device")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @PostMapping("/syncProductList")
    public AjaxResult syncProductList() {
        deviceService.synchronous();
        return AjaxResult.success();
    }

    @GetMapping("/allProduct")
    public R<List<ProductVo>> allProduct() {
        return R.ok(deviceService.allProduct());
    }

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterDto dto) {
        deviceService.register(dto);
        return AjaxResult.success();
    }

    @GetMapping("/list")
    public AjaxResult list(DeviceDto dto) {
        DevicePageVo vo = deviceService.list(dto);
        return AjaxResult.success().put("rows", vo.getRows()).put("total", vo.getTotal());
    }

    @GetMapping("/{iotId}")
    public R<DeviceDetailVo> queryDeviceDetail(@PathVariable String iotId) {
        return R.ok(deviceService.getDeviceByIotId(iotId));
    }

    @GetMapping("/queryServiceProperties/{iotId}")
    public R<List<PropertyVo>> queryServiceProperties(@PathVariable String iotId) {
        return R.ok(deviceService.queryServiceProperties(iotId));
    }

    @DeleteMapping("/{iotId}")
    public AjaxResult delete(@PathVariable String iotId) {
        deviceService.delete(iotId);
        return AjaxResult.success();
    }
}
